<?php
/**
 * GD Implementation of Bair Engine
 *
 * @package Bair
 * @subpackage Engine
 */
class Bair_Engine_Gd implements Bair_Engine_Interface
{
	public function __construct()
	{
		if( !function_exists('gd_info') )
		{
			throw new Bair_Exception("GD not installed!");
		}
	}
	
	public function getImageInfo($file)
	{
		if( !is_readable($file) )
			throw new Bair_Exception("Cannot read file '$file'");
			
		list($width, $height) = getimagesize($file);
		
		return new Bair_Image_Info($width, $height, null, $file);
	}
	
	public function resize($file, Bair_Image_Info $newImage)
	{
		if( !is_readable($file) )
			throw new Bair_Exception("Cannot read file '$file'");
		
		list($width, $height, $type, $attr) = getimagesize($file);
		
		$oldFile = null;
		$newFile = imagecreatetruecolor($newImage->getWidth(), $newImage->getHeight());
		
		switch( $type )
		{
			case IMAGETYPE_JPEG:
				
				$oldFile = imagecreatefromjpeg($file);
				
				break;
			case IMAGETYPE_GIF:
				
				$oldFile = imagecreatefromgif($file);
				
				if( ($transparentIndex = imagecolortransparent($oldFile)) > -1 )
				{
					$transparentColor = imagecolorsforindex($oldFile, $transparentIndex);
					$transparentIndex = imagecolorallocate($newFile, $transparentColor['red'], $transparentColor['green'], $transparentColor['blue']);
					
					imagecolortransparent($newFile, $transparentIndex);
				}
				
				if( $newImage->getType() === Bair::PNG )
				{
					imagealphablending($newFile, false);
					
					imagefill($newFile, 0, 0, $transparentIndex);
				}
				elseif( $newImage->getType() === Bair::GIF )
				{
					imagefill($newFile, 0, 0, $transparentIndex);
				}
				else
				{
					$colorWhite = imagecolorallocate($newFile, 255, 255, 255);
					imagefill($newFile, 0, 0, $colorWhite);
				}
				
				break;
			case IMAGETYPE_PNG:
				
				$oldFile = imagecreatefrompng($file);
				
				imagealphablending($oldFile, true);
				imagesavealpha($oldFile, true);
					
				if( ($transparentIndex = imagecolortransparent($oldFile)) > -1 )
				{
					$transparentColor = imagecolorsforindex($oldFile, $transparentIndex);
					$transparentIndex = imagecolorallocate($newFile, $transparentColor['red'], $transparentColor['green'], $transparentColor['blue']);
					
					imagecolortransparent($newFile, $transparentIndex);
				}
				
				if( $newImage->getType() === Bair::PNG )
				{
					$colorTransparent = imagecolorallocatealpha($newFile, 0, 0, 0, 127);
	   				imagefill($newFile, 0, 0, $colorTransparent);
				}
				elseif( $newImage->getType() === Bair::GIF )
				{
					imagefill($newFile, 0, 0, $transparentIndex);
				}
				else
				{
					$colorWhite = imagecolorallocate($newFile, 255, 255, 255);
					imagefill($newFile, 0, 0, $colorWhite);
				}
				
				break;
			case IMAGETYPE_WBMP:
				
				$oldFile = imagecreatefromwbmp($file);
				
				break;
			case IMAGETYPE_XBM:
				
				$oldFile = imagecreatefromxbm($file);
				
				break;
			default:
				
				$mime = image_type_to_mime_type($type);
				throw new Bair_Exception("File '$file' is of a type '$type' and is not supported by GD" );
				
				break;
		}
		
		imagecopyresampled($newFile, $oldFile, 0, 0, 0, 0, $newImage->getWidth(), $newImage->getHeight(), $width, $height);
		
		switch( $newImage->getType() )
		{
			case Bair::JPG:
				
				imagejpeg($newFile, $newImage->getFile(), $newImage->getOption('JPGQuality',100));
				
				break;
			case Bair::PNG:
				
				imagealphablending($newFile, true);
				imagesavealpha($newFile, true);
				
				imagepng($newFile, $newImage->getFile(), $newImage->getOption('PNGQuality',null), $newImage->getOption('filters',null));
				
				break;
			case Bair::GIF:
				
				imagegif($newFile, $newImage->getFile());
				
				break;
			default:
				throw new Bair_Exception("Invalid file type '{$newImage->getType()}'");
				break;
		}
		
		imagedestroy($oldFile);
		imagedestroy($newFile);
		
		return true;
	}
}